package com.psct.fragment;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ViewSwitcher.ViewFactory;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.widget.SearchView;
import com.psct.R;
import com.psct.adapter.GalleryAdapter;
import com.psct.bean.TextData;
import com.psct.ui.BookListActivity;
import com.psct.ui.TextShowActivity;
import com.psct.wiget.SearchListView;
import com.psct.wiget.SearchListView.SearchAdapter;

public class MainFragment extends SherlockFragment implements OnItemClickListener, OnClickListener,
ViewFactory, OnItemSelectedListener{
	public static final String TAG = "main";
	private Gallery gallery;
	private ImageSwitcher switcher;
	private Integer[] mImageIds = {
			// 定义整型数组 即图片源
			R.drawable.yy01_001, R.drawable.yy02_001, R.drawable.yy03_001, R.drawable.yy04_001,
			R.drawable.yy05_001, R.drawable.yy06_001, R.drawable.yy07_001, R.drawable.yy08_001,
			R.drawable.yy09_001, R.drawable.yy10_001, R.drawable.yy11_001, R.drawable.yy12_001, };

	private Handler handler = new Handler() {
	};
	/**
	 * 当前课本的坐标
	 */
	protected int curIndex;
	private List<TextData> textList ;
	private SearchListView sList;
	private ListView sListView;
	private SearchAdapter sAdapter;
	ActionBar actionBar;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_main, container,false);
		gallery = (Gallery) view.findViewById(R.id.gallery);
		switcher = (ImageSwitcher) view.findViewById(R.id.switcher);
		switcher.setFactory(this);
		switcher.setInAnimation(getActivity(), android.R.anim.fade_in);
		switcher.setOutAnimation(getActivity(), android.R.anim.fade_out);
		switcher.setOnClickListener(this);
		gallery.setAdapter(new GalleryAdapter(getActivity()));
		gallery.setOnItemSelectedListener(this);
		gallery.setSelection(Integer.MAX_VALUE / 2);
		return view;
	}
	@Override
	public View makeView() {
		ImageView i = new ImageView(getActivity());
		// 给ImageView设置资源
		WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
		// 显示的位置为:屏幕的宽度的一半-PopupWindow的高度的一半
		// int xPos =
		int imageSize = windowManager.getDefaultDisplay().getHeight() / 2;
		i.setLayoutParams(new ImageSwitcher.LayoutParams(imageSize, imageSize));
		return i;
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		curIndex = position % mImageIds.length;
		switcher.setImageResource(mImageIds[curIndex]);
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {

	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Animation bookAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_book_open);
		bookAnim.setFillAfter(true);
		handler.postDelayed(new Runnable() {
			public void run() {
				Intent startIntent = new Intent(getActivity(), BookListActivity.class);
				startIntent.putExtra("num", curIndex);
				getActivity().startActivity(startIntent);
				getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			}
		}, 200);
		switcher.startAnimation(bookAnim);
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(getActivity(), TextShowActivity.class);
		intent.putExtra("textId", textList.get(position).getId());
		intent.putExtra("textTitle", textList.get(position).getTitle());
		this.startActivity(intent);
	}
}
